<?php
/**
 * 
 * @version
 * @author duongthienduc
**/

class Admin_NewsController extends Bet_Admin_Controller {

	public function articleAddAction() {
		$articleForm = new Bet_Form_News_Article();
		if ($this->getRequest()->isPost()) {
			if ($articleForm->isValid($_POST)) {
				$article = new Bet_Model_NewsArticle($articleForm->getValues());

				$articleMapper = new Bet_Model_NewsArticleMapper();
				$result = $articleMapper->save($article);

				if ($result !== false) {
					$this->_redirect('/admin/news/articles.view');
				}
			}
		}

		$this->view->form = $articleForm;
	}

	public function articlesViewAction() {
		$newsArtMapper = new Bet_Model_NewsArticleMapper();
		$newsArticles = $newsArtMapper->fetchAllObject(null, 'news_article_id DESC');
		$this->view->assign("newsArticles", $newsArticles);
	}

	public function articleEditAction() {
		$newsArtMapper = new Bet_Model_NewsArticleMapper();
		$newArtForm = new Bet_Form_News_Article();
		if ($this->getRequest()->isPost()) {
			if ($newArtForm->isValid($_POST)) {
				$data = $newArtForm->getValues();
				if ($data['cid'] == 0) {
					throw new Bet_Exceptions_InvalidParamsException("Invalid news article item");
				}
				$newsArticle = new Bet_Model_NewsArticle($newArtForm->getValues());
				$newsArticle->setNewsArticleId($data['cid']);

				$result = $newsArtMapper->save($newsArticle, true);

				if ($result !== false) {
					$this->_redirect('/admin/news/articles.view');
				}
			}
		} else {
			$newsArtId = $this->getRequest()->getParam('cid', 0);
			if ($newsArtId == 0) {
				throw new Bet_Exceptions_InvalidParamsException("Invalid news article item");
			}

			$newsArtData = $newsArtMapper->findByIdToArray($newsArtId);
			if ($newsArtId == 0) {
				throw new Bet_Exceptions_InvalidParamsException("Invalid news article item");
			}

			$newsArtData['cid'] = $newsArtId;
			$newsArtData['newsCategoryId'] = $newsArtData['news_category_id'];
			$newArtForm->populate($newsArtData);
		}

		$this->view->form = $newArtForm;
		$this->render('article.add', null, false);
	}

	public function categoryAddAction() {
		$newCatForm = new Bet_Form_News_Category();
		if ($this->getRequest()->isPost()) {
			if ($newCatForm->isValid($_POST)) {
				$newsCategory = new Bet_Model_NewsCategory($newCatForm->getValues());

				$categoryMapper = new Bet_Model_NewsCategoryMapper();
				$result = $categoryMapper->save($newsCategory);

				if ($result !== false) {
					$this->_redirect('/admin/news/categories.view');
				}
			}
		}

		$this->view->form = $newCatForm;
	}

	public function categoryEditAction() {
		$newsCategoryMapper = new Bet_Model_NewsCategoryMapper();
		$newCatForm = new Bet_Form_News_Category();
		if ($this->getRequest()->isPost()) {
			if ($newCatForm->isValid($_POST)) {
				$data = $newCatForm->getValues();
				if ($data['cid'] == 0) {
					throw new Bet_Exceptions_InvalidParamsException("Invalid news category item");
				}
				$newsCategory = new Bet_Model_NewsCategory($newCatForm->getValues());
				$newsCategory->setNewsCategoryId($data['cid']);

				$result = $newsCategoryMapper->save($newsCategory, true);

				if ($result !== false) {
					$this->_redirect('/admin/news/categories.view');
				}
			}
		} else {
			$newsCatId = $this->getRequest()->getParam('cid', 0);
			if ($newsCatId == 0) {
				throw new Bet_Exceptions_InvalidParamsException("Invalid news category item");
			}

			$newsCategoryData = $newsCategoryMapper->findByIdToArray($newsCatId);
			$newsCategoryData['cid'] = $newsCategoryData['news_category_id'];

			$newCatForm->populate($newsCategoryData);
		}

		$this->view->form = $newCatForm;
		$this->render('category.add', null, false);
	}

	public function categoriesViewAction() {
		$newsCatMapper = new Bet_Model_NewsCategoryMapper();
		$newsCats = $newsCatMapper->fetchAllObject(null, 'order ASC');
		$this->view->assign("newsCats", $newsCats);
	}

}
?>